<template>
  <div class="content">
    <!-- <h1>职位管理/添加</h1> -->
    <div class="detail">
      <div class="head"><span>添加 </span></div>
      <div class="info">
        <div class="tittle">职位信息</div>
        <div class="name flex-start">
          <div class="left">
            <span class="c-error">*</span><span>职位名称</span>
          </div>
          <input
            type="text"
            class="searchInput w_420"
            placeholder="请输入职位名称"
            v-model="roleName"
          />
        </div>
        <div class="describe flex-start">
          <div class="left miaoshu">职位描述</div>
          <el-input
            type="textarea"
            placeholder="请输入版本信息"
            v-model="text"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </div>
      </div>
      <div class="flex-start setting">
        <input type="checkbox" v-model="checked" />
        <span class="c-error">*</span><span>职位设置</span>
      </div>
    </div>
    <div
      v-show="checked === true"
      class="item"
      v-for="item in lists"
      :key="item.level1Name"
    >
      <div class="shezhi">
        <input
          type="checkbox"
          v-model="item.checked"
          @click="changeleve2state(item)"
        />
        <span>{{ item.level1Name }}</span>
      </div>
      <div class="zhedie">
        <div class="pic">
          <img
            :src="item.flag === false ? imgUrl1 : imgUrl2"
            alt=""
            @click="open(item.level1Name, $event)"
          />
        </div>
      </div>
      <ul v-show="item.flag === true">
        <li
          v-for="items in item.children"
          :key="items.level2Name"
          :class="{ inline: !items.children }"
        >
          <div v-if="items.children" class="border flex-start">
            <input
              type="checkbox"
              v-model="items.checked"
              @click="abc([item, items])"
            />
            <span>{{ items.level2Name }}</span>
            <div class="zhedie top10">
              <div class="pic">
                <img
                  :src="items.flag === false ? imgUrl1 : imgUrl2"
                  alt=""
                  @click="open2([item, items])"
                />
              </div>
            </div>
          </div>
          <div v-else class="flex-start">
            <span
              :class="[
                items.children ? '' : 'finall',
                items.checked ? 'active' : '',
              ]"
              @click="abc([item, items])"
              >{{ items.level2Name }}</span
            >
          </div>
          <div v-show="items.flag === true">
            <div v-if="items.children" class="flex-start top30">
              <span
                v-for="a in items.children"
                :key="a.level3Name"
                :class="['finall', a.checked ? 'active' : '']"
                @click="changeState([item, items, a])"
                >{{ a.level3Name }}</span
              >
            </div>
          </div>
        </li>
        <div
          v-if="item.level1Name === lists[lists.length - 1].level1Name"
          class="btn-blue3"
          @click="confirm"
        >
          确定
        </div>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      text: '',
      roleName: '',
      checked: true,
      imgUrl1: require('@/assets/open.png'),
      imgUrl2: require('@/assets/fold.png')
    }
  },
  computed: {
    lists () {
      const b = this.$store.state.position.authorList
      for (let i = 0; i < b.length; i++) {
        this.$set(b[i], 'flag', true)
        this.$set(b[i], 'level1Name', b[i].label)
        this.$set(b[i], 'checked', false)
        if (b[i].children) {
          for (let j = 0; j < b[i].children.length; j++) {
            this.$set(b[i].children[j], 'flag', true)
            this.$set(b[i].children[j], 'level2Name', b[i].children[j].label)
            this.$set(b[i].children[j], 'checked', false)
            if (b[i].children[j].children) {
              for (let k = 0; k < b[i].children[j].children.length; k++) {
                this.$set(b[i].children[j].children[k], 'level3Name', b[i].children[j].children[k].label)
                this.$set(b[i].children[j].children[k], 'checked', false)
              }
            }
          }
        }
      }
      return b
    }
  },
  // watch: {
  //   '$store.state.position.authorList': function () {

  //   }

  // },
  created () {
    // console.log(this.lists)
    this.$store.dispatch('position/positionSelectData').catch(res => {
      this.$message({
        type: 'error',
        message: res,
        duration: 1000
      })
    })
  },
  methods: {
    open (val, e) {
      // // console.log(e.target.parentNode.parentNode)
      this.lists.find(
        (item) => item.level1Name === val
      ).flag = !this.lists.find((item) => item.level1Name === val).flag
      // // console.log(this.lists)
    },
    open2 (val) {
      // this.$store.commit('position/set_level2flag', val)
      const level1 = this.lists.find(
        (item) => item.level1Name === val[0].level1Name
      ).children
      const level2 = level1.find(
        (item) => item.level2Name === val[1].level2Name
      )
      level2.flag = !level2.flag
    },
    changeleve2state (val) {
      // // console.log(val.checked)
      this.$store.commit('position/set_level1Checked', val)
    },
    abc (val) {
      // // console.log(val)
      // // console.log(val)
      this.$store.commit('position/set_level2Checked', val)
    },
    changeState (val) {
      this.$store.commit('position/set_level3Checked', val)
    },
    confirm () {
      // console.log(this.lists)

      const result = []
      const getRes = (arr) => {
        arr.forEach(v => {
          v.checked && !v.children && result.push(v.id)
          if (v.children instanceof Array) {
            getRes(v.children)
          }
        })
      }
      getRes(this.lists)

      // 获取rolePermissions
      const rolePermissionsRes = []
      const getrolePermissions = (arr) => {
        arr.forEach(v => {
          !v.children && rolePermissionsRes.push(v.checked)
          if (v.children instanceof Array) {
            getrolePermissions(v.children)
          }
        })
      }
      getrolePermissions(this.lists)
      // console.log(rolePermissionsRes)
      const isAll = rolePermissionsRes.every(item => {
        return item === true
      })
      // console.log(isAll)
      let rolePermissions
      if (isAll) {
        rolePermissions = '*'
      } else {
        result.push(['personalData:update:password', 'personalData:update:avatar',
          'personalData:login', 'personalData:logout'])
        rolePermissions = result.join(',')
      }
      // console.log(rolePermissions)
      if (this.roleName && rolePermissions) {
        this.$store.dispatch('systemManage/addPositionData', { roleDesc: this.text, roleName: this.roleName, rolePermissions: rolePermissions }).then(res => {
          this.$message({
            type: 'success',
            message: res.message,
            duration: 1000
          })
          this.$router.push('/systemManage/levelManage')
        }).catch(res => {
          // console.log(res)
          this.$message({
            type: 'error',
            message: res.message,
            duration: 1000
          })
        })
      } else {
        this.$message({
          type: 'error',
          message: '请填写必填项',
          duration: 1000
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  height: 500px;
  box-sizing: border-box;
  background: #ffffff;
  border-radius: 8px;
  // margin-top: 24px;
  padding: 0 24px;
}
.head {
  width: 100%;
  height: 78px;
  border-bottom: 1px solid #e6e9f0;
  position: relative;
  font-size: 16px;
  font-weight: 500;
  color: #1b1f35;
  span {
    position: absolute;
    bottom: 20px;
  }
}
.tittle {
  margin-top: 35px;
  margin-left: 30px;
  font-size: 16px;
  font-family: PingFangSC, PingFangSC-Medium;
  font-weight: 500;
  color: #1b1f35;
}
.name {
  width: 520px;
  margin-top: 41px;
  margin-left: 28px;
  .w_420 {
    width: 420px;
  }
}
.describe {
  width: 520px;
  margin-top: 20px;
  margin-left: 34px;

  /deep/ .el-textarea .el-textarea__inner {
    margin-left: 8px;
    width: 420px;
    height: 164px;
    position: relative;
    color: #333333;
    font-family: PingFangSC, PingFangSC-Regular;
    //   margin-top: 8px;
    margin-bottom: 20px;
    resize: none;
    outline: none;
    // border: 1px solid #c6ccd7;
  }
  /deep/ .el-textarea .el-input__count {
    z-index: 999;
    bottom: 30px;
    right: 20px;
    background: none;
    // left: 200px;
  }
}
.left {
  display: inline-block;
  width: 80px;
  margin-right: 20px;
}
.miaoshu {
  height: 140px;
}
.setting {
  margin-left: 10px;
  input {
    margin-right: 10px;
  }
}
.item {
  // padding: 10px 40px;
  position: relative;
  .shezhi {
    line-height: 18px;
    margin-left: 58px;
    margin-top: 10px;
    margin-bottom: 10px;
    span {
      vertical-align: top;
      margin-left: 10px;
      // line-height: 14px;
    }
  }
  .zhedie {
    position: absolute;
    right: 40px;
    top: 0px;
    .pic {
      width: 16px;
      height: 16px;
      img {
        width: 100%;
      }
    }
  }
  ul {
    padding: 40px 60px;
    background-color: #fff;
    border-radius: 8px;
    .finall {
      padding: 10px;
      border: 1px solid #a5aebf;
      border-radius: 5px;
      color: #3d4966;
      margin-right: 40px;
      // margin-bottom: 40px;
      display: inline-block;
    }
  }
}
.active {
  border: 1px solid #304eec !important;
}
.inline {
  display: inline-block;
}
.border {
  height: 36px;
  border-bottom: 1px solid #e6e9f0;
  position: relative;
  span {
    vertical-align: top;
    margin-left: 10px;
  }
}
.top30 {
  margin-top: 30px;
  margin-bottom: 20px;
}
.btn-blue3 {
  margin-top: 60px;
  margin-bottom: 100px;
}
.top10 {
  top: 10px !important;
  right: 0px !important;
}
</style>
